<?php


namespace App\Transforms;


use App\Enum\CarEnum;
use App\Enum\DriverEnum;
use App\Models\CarBrand;
use App\Models\DriverWallet;
use App\Models\Orders;
use App\Models\ServiceType;
use App\Models\User;
use App\Repositories\CommentRepository;
use App\Repositories\CompanyRepository;
use App\Repositories\ServiceRepository;
use App\Repositories\UploadFileRepository;
use BobCoder\Construct\Foundation\Transform\TransformContract;
use Illuminate\Support\Facades\DB;

class DriverTransformer implements TransformContract
{

    public function transform($attributes)
    {
        if (!$attributes) return $attributes;

        // 身份证照片
        if ($attributes->identity_photo) {
            if(!is_array($attributes->identity_photo)) $attributes->identity_photo = json_decode($attributes->identity_photo,true);
            $attributes->identity_photo = (new UploadFileRepository())->getFilePathList($attributes->identity_photo);
        }

        // 驾照照片
        if ($attributes->licence_photo) {
            if(!is_array($attributes->licence_photo)) $attributes->licence_photo = json_decode($attributes->licence_photo,true);
            $attributes->licence_photo = (new UploadFileRepository())->getFilePathList($attributes->licence_photo);
        }

        // 车辆信息
        if ($attributes->car) {
            foreach ($attributes->car as &$item) {
                $item['type'] = ServiceType::find($item['type'])->name;
                $item['ownership'] = $item['ownership'] == CarEnum::PERSON ? '个人' : '公司';
                $item['host'] = $item['host'] == 1 ? '是' : '否';
                $company = (new CompanyRepository())->find($item['company_id'], ['name']);
                $brand = CarBrand::find($item['brand_id'], ['name']);
                $item['brand'] = $brand ? $brand['name'] : '';
                $item['company'] = $company ? $company['name'] : '';
            }
        }

        // 服务评分
        $attributes->score = [];
        if ($attributes->work_scope) {
            $data = [];
            foreach ($attributes->work_scope as $item) {
                $name = (new ServiceRepository())->getNameById($item);
                $data[] = [
                    'name' => $name,
                    'value' => (new CommentRepository())->scoreAvg(['driver_id' => $attributes->id, 'service_id' => (int)$item])
                ];
            }
            $attributes->score = $data;
        }

        // 提现信息
        if ($attributes->wallet) {
            foreach ($attributes->wallet as &$item) {
                $item->type = DriverEnum::payMessage($item->type);
            }
        }

        // TODO 订单、服务等信息
        $count = $this->count($attributes->id, '0');
        $attributes->balance = $count['driving']; // 余额
        $attributes->order = $count['count']; // 完成订单数
        $attributes->mileage = $count['km']; // 里程数
        $attributes->income = $count['driver_money']; // 总收入
        $attributes->complain = 0; // 投诉次数

        return $attributes;
    }

    public function transformDetails($attributes)
    {
        if (!$attributes) return $attributes;

        // 身份证照片
        if ($attributes->driver_others->identity_photo) {
            if(!is_array($attributes->driver_others->identity_photo)) $attributes->driver_others->identity_photo = json_decode($attributes->driver_others->identity_photo,true);
            $attributes->identity_photo = (new UploadFileRepository())->getFilePathList($attributes->driver_others->identity_photo);
        }

        // 驾照照片
        if ($attributes->driver_others->licence_photo) {
            if(!is_array($attributes->driver_others->licence_photo)) $attributes->driver_others->licence_photo = json_decode($attributes->driver_others->licence_photo,true);
            $attributes->licence_photo = (new UploadFileRepository())->getFilePathList($attributes->driver_others->licence_photo);
        }

        // 服务评分
        $attributes->score = [];
        if ($attributes->work_scope) {
            $data = [];
            foreach ($attributes->work_scope as $item) {
                $name = (new ServiceRepository())->getNameById($item);
                $data[] = [
                    'name' => $name,
                    'value' => (new CommentRepository())->scoreAvg(['driver_id' => $attributes->id, 'service_id' => (int)$item])
                ];
            }
            $attributes->score = $data;
        }

        // 提现信息
        if ($attributes->wallet) {
            foreach ($attributes->wallet as &$item) {
                $item->type = DriverEnum::payMessage($item->type);
            }
        }

        // TODO 订单、服务等信息
        $count = $this->count($attributes->id, '1');
        $attributes->balance = $count['driving']; // 余额
        $attributes->order = $count['count']; // 完成订单数
        $attributes->mileage = $count['km']; // 里程数
        $attributes->income = $count['driver_money']; // 总收入
        $attributes->complain = 0; // 投诉次数

        return $attributes;
    }
    public function transformDetails_info($attributes)
    {
        if (!$attributes) return $attributes;
        // 身份证照片
        if ($attributes->identity_photo) {
            if(!is_array($attributes->identity_photo)) $attributes->identity_photo = json_decode($attributes->identity_photo,true);
            $attributes->identity_photo = (new UploadFileRepository())->getFilePathList($attributes->identity_photo);
        }

        // 驾照照片
        if ($attributes->licence_photo) {
            if(!is_array($attributes->licence_photo)) $attributes->licence_photo = json_decode($attributes->licence_photo,true);
            $attributes->licence_photo = (new UploadFileRepository())->getFilePathList($attributes->licence_photo);
        }
        $attributes->score = [];
        if ($attributes->work_scope) {
            $data = [];
            foreach ($attributes->work_scope as $item) {
                $name = (new ServiceRepository())->getNameById($item);
                $data[] = [
                    'name' => $name,
                    'value' => (new CommentRepository())->scoreAvg(['driver_id' => $attributes->id, 'service_id' => (int)$item])
                ];
            }
            $attributes->score = $data;
        }
        /**
         * 用户头像
         */
        if($attributes->driver)
        {
         $user =  User::query()->select('avatar')->find($attributes->driver->user_id);
        }

        // 提现信息
        if ($attributes->wallet) {
            foreach ($attributes->wallet as &$item) {
                $item->type = DriverEnum::payMessage($item->type);
            }
        }
        // TODO 订单、服务等信息
        $count = $this->count($attributes->id, '1');
        $attributes->balance = $count['driving']; // 余额
        $attributes->order = $count['count']; // 完成订单数
        $attributes->mileage = $count['km']; // 里程数
        $attributes->income = $count['driver_money']; // 总收入
        $attributes->complain = 0; // 投诉次数
        $attributes->avatar = $user->avatar; // 司机头像
        return $attributes;

    }


    /**
     * 统计
     */
    public function count(int $driver_id, string $type)
    {
        $count = Orders::query()
            ->select(DB::raw("sum(minute) as minute,sum(km) as km,count(id) as count ,sum(driver_money) as driver_money"))
            ->where('driver_id', $driver_id)
            ->whereIn('status', ['4', '5', '6'])
            ->where('type', $type)
            ->get();
        $amount = DriverWallet::query()->where([['driver_id', $driver_id], ['type', '0']])->first('amount');
        $driving = DriverWallet::query()->where([['driver_id', $driver_id], ['type', '1']])->first('amount');
        $count[0]['driver_id'] = $driver_id; //司机ID
        $count[0]['online'] = $amount ? $amount->amount : 0; //网约车钱包
        $count[0]['driving'] = $driving ? $driving->amount : 0; //网约车钱包
        if (is_null($count[0]['driver_money'])) $count[0]['driver_money'] = 0;
        if (is_null($count[0]['minute'])) $count[0]['minute'] = 0;

        return $count[0];
    }

    /**
     * 格式化数据
     * @param $list
     * @return mixed
     * @Author 姿势就是力量
     */
    public function transformList($list)
    {

        if (!$list) return $list;


        foreach ($list as &$item) {

            if (isset($item['company']) && $item['company']) {
                $item['company'] = $item['company']['name'];
            }
            if (isset($item['car']) && $item['car']) {
                $item['car'] = implode(',', array_column($item['car'], 'number'));
            }
            if (isset($item['city']) && $item['city']) {
                $item['city'] = str_replace(',', '', $item['city']['merger_name']);
            }

            if (isset($item['auditor']) && $item['auditor']) {
                $item['auditor'] = $item['auditor']['name'];
            }
            $item['status'] = DriverEnum::statusMessage($item['status']); //网约车状态
            $item['taxi_status_state'] = $item['taxi_status']; //出租车状态
            $item['taxi_status'] = DriverEnum::statusMessage($item['taxi_status']); //出租车状态
            // TODO 订单数量统计
            $item['order'] = Orders::query()->where('type', '0')->where('driver_id', $item['id'])->whereIn('status', ['4', '5', '6'])->count();
        }

        return $list;
    }
}
